<template>
  <div ref="chartDom" :class="className" :style="{ height: height, width: width }"></div>
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons");

export default {
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
    unit: {
      type: String,
      default: "吨",
    },
    dataName: {
      type: String,
      default: "减排CO₂",
    },
  },
  data() {
    return {
      chart: null,
      dataList: [0, 0, 0, 0, 650000],
      dataList1: [0, 0, 0, 0, 105535],
      resizeObserver: null
    };
  },
  mounted() {
    this.initChart();
    this.initResizeObserver();
  },
  beforeDestroy() {
    if (this.resizeObserver) {
      this.resizeObserver.disconnect();
    }
    if (this.chart) {
      this.chart.dispose();
      this.chart = null;
    }
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$refs.chartDom, "macarons");

      // 调整grid配置，确保图表填满容器
      const option = {
        legend: {
          textStyle: { color: "#ffffff" },
          right: 0,
          itemHeight: 5,
          itemWidth: 20,
        },
        grid: {
          top: 45,    // 减少顶部间距
          left: 27,
          right: 10,  // 添加右侧间距
          bottom: 5, // 减少底部间距
          containLabel: true // 确保坐标轴标签在grid内
        },
        xAxis: {
          type: "category",
          data: ["2021", "2022", "2023", "2024", "2025"],
          axisTick: { show: false },
          axisLabel: {
            interval: 0 // 强制显示所有标签
          }
        },
        yAxis: {
          type: "value",
          splitLine: {
            lineStyle: {
              type: "dashed",
              color: "#B0D7FF33",
            },
          },
          name: "单位：万m³、万吨",
          // 添加格式化显示
          axisLabel: {
            formatter: function (value) {
              // 将数值除以10000并添加单位
              return value / 10000;
            }
          },
          nameTextStyle: {
            color: "#999999",
            padding: [0, 0, 10, 0] // 调整名称位置
          },
        },
        tooltip: { trigger: 'axis' },
        series: [
          {
            name: "减排CO₂",
            data: this.dataList,
            type: "line",
            areaStyle: {},
            itemStyle: { color: "#36F5F74D" },
            z: 10,
          },
          {
            name: "节约标煤",
            data: this.dataList1,
            type: "line",
            areaStyle: {},
            itemStyle: { color: "#0AA7FF4D" },
            z: 10,
          }
        ]
      };

      this.chart.setOption(option);
    },
    initResizeObserver() {
      // 使用ResizeObserver监听容器尺寸变化
      this.resizeObserver = new ResizeObserver(() => {
        this.handleResize();
      });
      this.resizeObserver.observe(this.$refs.chartDom);
    },
    handleResize() {
      if (this.chart) {
        this.chart.resize();
      }
    }
  }
};
</script>

<style scoped>
.chart {
  position: relative;
  display: block;
  overflow: hidden;
  height: 100%;
  width: 100%;
}

/* 确保父容器有正确的高度 */
.line-chart-container {
  height: 100%;
  width: 100%;
  position: relative;
}
</style>
